package control;

import javax.microedition.lcdui.Graphics;

import main.Res;

public class TextControl extends Control{
	int x,y;
	String text;
	int fontHeight = font.getHeight();
	public void setText(String text)
	{
		this.text = text;
	}
	public TextControl(String text,int x,int y)
	{
		this.x = x;
		this.y = y;
		this.text = text;
	}
	protected void drawString(Graphics g,String text,int x,int y)
	{
		int index = 0;
		
		int startIndex = 0;
		do
		{
			index = text.indexOf("$(",startIndex);
			if(index>-1)
			{
				if(index>0)
				{
					String subString = text.substring(startIndex, index);
					g.drawString(subString, x, y, Graphics.LEFT|Graphics.TOP);
					x+=font.stringWidth(subString);
				}
				startIndex = index+2;
				index = text.indexOf(')',startIndex);
				Integer icon = Integer.valueOf(text.substring(startIndex, index));
				//Res.drawSmallIcon(g, icon.intValue(), x, y+fontHeight-Res.SMALL_ICON_SIZE);
				Res.drawIcon(g, icon.intValue(), x, y+fontHeight-32);
				x+=32;
				startIndex = index+1;
			}
		}while(index>-1);
		if(startIndex<text.length())
		{
			g.drawString(text.substring(startIndex), x, y, Graphics.LEFT|Graphics.TOP);
		}
	}
	protected void controlPaint(Graphics g){
		g.setColor(COLOR_TEXT);
		g.setFont(font);
		
		int startIndex = 0;
		int index = 0;
		int dy=0;
		do{
			index = text.indexOf('\n',startIndex);
			if(index>0)
			{
				drawString(g,text.substring(startIndex, index),x, y+dy);
				dy+=fontHeight;
				startIndex = index+1;
				//text = text.substring(index+1);
			}
		}while(index>0);
		if(startIndex<text.length())
		{
			drawString(g,text.substring(startIndex),x, y+dy);
		}
		/*
		for (int i = 0; i < strings.length; i++) {
			drawString(g,strings[i], x, y+i*fontHeight);
		}*/
	}
}
